﻿using SalesSys.BLL;
using SalesSys.DAL.Models;
using SalesSys.WebCore;
using SalesSys.WebCore.Authorize;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace SalesSys.Site.Controllers.Api
{
    /// <summary>
    /// 消息
    /// </summary>
    [RoutePrefix("api/NoticeInfo")]
    public class NoticeInfoController : BaseApiController
    {
        NoticeInfoBLL entityBLL = BLL.BllFactory.GetBll<NoticeInfoBLL>();

        /// <summary>
        /// 获取个人消息
        /// </summary>
        /// <param name="page">分页</param>
        /// <param name="rows">每页条数</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns></returns>
        [Route("MyList")]
        public ViewDtos<NoticeInfo> Get(DateTime? startTime = null, DateTime? endTime = null, int page = 1, int rows = 10)
        {
            var roles = ClientHelper.ClientInfo.GetRoles();
            var msgs=entityBLL.GetPaging(p => p.To == ClientHelper.ClientInfo.Id || roles.Contains(p.ToRoles), p => p.CreateTime, true, page, rows);  
           
            if (startTime != null && endTime != null)
            {
                startTime = startTime.Value.Date;
                endTime = endTime.Value.Date.AddDays(1);
                msgs = entityBLL.GetPaging(p => (p.To == ClientHelper.ClientInfo.Id || roles.Contains(p.ToRoles)) && p.CreateTime >= startTime && p.CreateTime <= endTime, p => p.CreateTime, true, page, rows);
               
            }
            if (startTime == null && endTime != null)
            {
                endTime = endTime.Value.Date.AddDays(1);
                msgs = entityBLL.GetPaging(p => (p.To == ClientHelper.ClientInfo.Id || roles.Contains(p.ToRoles)) && p.CreateTime <= endTime, p => p.CreateTime, true, page, rows);

            }
            if (startTime != null && endTime == null)
            {
                startTime = startTime.Value.Date;
                msgs = entityBLL.GetPaging(p => (p.To == ClientHelper.ClientInfo.Id || roles.Contains(p.ToRoles)) && p.CreateTime >= startTime, p => p.CreateTime, true, page, rows);

            }
            var dto = ViewDto.CreateList(msgs.Value);
            dto.Total = msgs.Key;
            dto.Page = page;
            dto.Rows = rows;
            return dto;
        }

    }
}
